$(function(){
    $('#loginBox a').on('click',function(){
        $('#regBox').show()
        $('#loginBox').hide()
    })
    $('#regBox a').on('click',function(){
        $('#loginBox').show()
        $('#regBox').hide()
    })


    let form = layui.form;
    form.verify({
        username:[
            /^[\S]{1,10}$/,
            '用户名需要1-10'
        ],
        pwd:[
            /^[\S]{6,15}$/,
            '用户名需要1-10'
        ],
        rePwd:function(value){
            let pwd = $('#regBox input[name=password]').val()
            if(pwd !== value){
                return '两次密码不一样'
            }
        }
        
    })
    $('#formReg').on('submit',function(e){
        e.preventDefault()
        axios({
            method:'POST',
            url:'/api/reguser',
            data:$(this).serialize(),
        }).then (res => {
           console.log(res.data);
        if(res.data.status !==0){
            return layui.layer.msg(res.data.message)
        }
            layui.layer.msg('恭喜您注册成功')
            $('#regBon a').click()
            $('#formReg')[0].reset()
        })
    })
    $('#formLogin').on('submit',function(e){
        e.preventDefault()
        axios({
            method:'POST',
            url:'/api/login',
            data:$(this).serialize(),
        }).then(res=>{
            // console.log(res.data);
            if(res.data.status !==0 ){
                return layui.layer.msg(res.data.message)
            }
            layui.layer.msg('恭喜你啊，进去了')
            localStorage.setItem('token',res.data.token)
            location.href='/index.html'
        })
    })

})

